package com.cc.crm.service.impl;

import com.cc.crm.service.IReptService;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.List;


@Service
public class ReptServiceImpl implements IReptService {
    @Override
    public void writeJFreeChartToOsCons(OutputStream os) {
        DefaultPieDataset ret = new DefaultPieDataset();

        //这里改成客户构成数据：客户登记，客户数量
        ret.setValue("阿里巴巴", 35.0);
        ret.setValue("腾讯", 10);
        ret.setValue("京东", 25);
        ret.setValue("百度", 5);
        ret.setValue("新浪", 15);


        
        String title = "客户构成分析饼图";
        Font bigFont = new Font("宋体", Font.BOLD, 22);
        //Font middleFont = new Font("黑体", Font.BOLD, 14);

        JFreeChart chart = ChartFactory.createPieChart(title, ret, true,
                false, false);
        Font LegendFont = new Font("楷体", Font.PLAIN, 18);  
        LegendTitle legend = chart.getLegend(0);  
        legend.setItemFont(LegendFont);
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("微软雅黑",Font.BOLD,12));
        plot.setBackgroundAlpha(0.2f);
        plot.setNoDataMessage("无查询结果报表信息！");
        plot.setCircular(true);

        // 图片中显示百分比:自定义方式，{0} 表示选项， {1} 表示数值， {2} 表示所占比例 ,小数点后两位
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0}={1}({2})", NumberFormat.getNumberInstance(),
                new DecimalFormat("0.00%")));
        // 图例显示百分比:自定义方式， {0} 表示选项， {1} 表示数值， {2} 表示所占比例
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
        // 设置第一个饼块截面开始的位置，默认是12点钟方向
        plot.setStartAngle(90);
        
        chart.setTitle(new TextTitle(title,bigFont));
        //==============官方案例是通过CharFrame将图标显示在窗体中，而这里我们需要将图标
        //显示到网页中，必须BufferedImage类将图标的二进制输出到网页=========
        //jfc对象已经存在
        BufferedImage bi = chart.createBufferedImage(500, 370);
        try {
            ImageIO.write(bi, "PNG", os);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public List getPidStatisticsCons() {
        //String hql = "select s.stuEducation,count(s.stuEducation) from Student s group by s.stuEducation";
        List ret = Arrays.asList(new String[] {"阿里巴巴","1"},new String[] {"腾讯","1"},new String[] {"京东","1"});
        return ret;
    }
}
